<?php
//copie des fichiers
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['package']))
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
else
{
	$temp_path = $project_path.'/temp/';
	$file = $_SESSION['install']['package']['name'].'.'.str_replace('.', '_', $_SESSION['install']['package']['version']).'.update.tgz';
	if(!file_exists($temp_path.$file) || filesize($temp_path.$file)!=$_SESSION['install']['package']['size'] || md5_file($temp_path.$file)!=$_SESSION['install']['package']['md5'])
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
		$error = true;
	}
}
fwDbEngin::$dieOnError = false;
	
function createDir($path)
{
	global $project_path;
	if(!fwTools::mkdirTree($project_path, $path))
	{
		echo 'updateDisplayError("«!ADMIN_NSTALL_ERROR_EXECUTINGSCRIPT»");';
		return false;
	}
	return true;
}

function createFile($file_name, $data)
{
	global $project_path;
	createDir(dirname($file_name));
	if(!@file_put_contents($project_path.$file_name, $data))
	{
		echo 'updateDisplayError("«!ADMIN_NSTALL_ERROR_EXECUTINGSCRIPT»");';
		return false;
	}
	return true;
}
function executeSql($query)
{
	if(fwDb::setData($query)===false)
	{
		echo 'updateDisplayError("«!ADMIN_NSTALL_ERROR_EXECUTINGSCRIPT»");';
		return false;
	}
	return true;
}

if(!$error)
{
	$setup = new SimpleXMLElement($_SESSION['install']['package']['index']);
	include_once($project_path.'/class/ext/archive/gzip.class.php5');
	$package = $_SESSION['install']['package'];
	$file = $package['name'].'.'.str_replace('.', '_', $package['version']).'.update.tgz';
	$tgz = new gzip_file($temp_path.$file);
	$tgz->set_options(array('basedir' => $temp_path, 'overwrite' => 1));

	if(isset($_SESSION['install']['memo']))
	{
		$index = $_SESSION['install']['memo'];
		unset($_SESSION['install']['memo']);
	}
	else
	{
		$index = 0;
	}
	session_write_close();
	if(count($setup->scripts)>0)
	{
		$nb = count($setup->scripts[0]->script);
		$last_progree= microtime(true);

		for($i=$index; $i<$nb; $i++)
		{
			$script = $setup->scripts[0]->script[$i];
			if((microtime(true)-$last_progree)*1000>470)
			{
				session_start();
				if((microtime(true)-$start_time)>23)
				{
					$_SESSION['install']['memo'] = $index;
					echo 'updatePerform();';
					$error = true;
					break;
				}
				$_SESSION['install']['progress'] = round(($index*1000)/$nb);
				if($_SESSION['install']['cancel'])
				{
					$error = true;
					break;
				}
				session_write_close();
				$last_progree= microtime(true);
			}
			if($script['event']=='aftercopy')	
			{
				sleep(1);
				//@todo tester le patch
				switch($script['type'])
				{
					case 'mkdir':
						if(!@createDir($script['path']))
						{
							$error = true;
							break;
						}
						break;
					case 'mkfile':
						if(!@createFile($script['path'], trim(''.$script)))
						{
							$error = true;
							break;
						}
						break;
					case 'sql':
						if(!@executeSql(''.$script))
						{
							$error = true;
							break;
						}
						break;
					case 'patch':
						$file_patch = $script['path'];
						list($errors, $n_succes) = $tgz->extract_files(array(array('', $file_patch)));

						if(count($errors)>0 || !is_file($temp_path.'/'.$file_patch))
						{
							echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_EXTRACTING»");';
							$error = true;
						}
						else
						{
							$_SESSION['install']['patch_execute'] = time();
							include($temp_path.'/'.$file_patch);
							unset($_SESSION['install']['patch_execute']);
							
							@unlink($temp_path.'/'.$file_patch);
						}
						break;
				}
			}
			if($error)
			{
				break;
			}
		}
	}
}
if(!$error)
{
	session_start();
	include($project_path.'/install_report.php5');
	$conf['appli']['packages'][$_SESSION['install']['package']['name']]['version'] = $_SESSION['install']['package']['version'];
	$conf['appli']['packages'][$_SESSION['install']['package']['name']]['history'][$_SESSION['install']['package']['version']] = date('Y-M-d H:i:s');
	fwTools::saveConfig('appli');

	$_SESSION['install']['screen'] = 2;
	$_SESSION['install']['progress'] = 0;
	$_SESSION['install']['status'] = '';
	echo 'updateUpdateStepCurrent("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", null)
updateUpdateStepDone("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", 6)
updateStopProgress();
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_CHECK_HEAD»", "«!ADMIN_INSTALL_CHECK_STATUS»");';
	@unlink($temp_path.$file);
	unset($_SESSION['install']['package']);
}
?>